package org.example.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.config.R;
import org.example.domain.Notifis;
import org.example.service.NotifisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("notifis")
public class NotifisController {
    @Autowired
    NotifisService notifisService;
    @Autowired
    RedisTemplate redisTemplate;
    @RequestMapping("notifisList")
    public R notifisList(@RequestBody Notifis notifis, Page page){
        PageHelper.startPage((int) page.getCurrent(), (int) page.getSize());
        List<Notifis> list = notifisService.notifisList(notifis);
        PageInfo<Notifis> notifisPageInfo = new PageInfo<>(list);
        redisTemplate.opsForValue().setIfAbsent("notifisList",notifisPageInfo,5, TimeUnit.MINUTES);
        return R.successed(notifisPageInfo);
    }

    @RequestMapping("notifisAdd")
    public R notifisAdd(@RequestBody Notifis notifis,String username){
        return notifisService.notifisAdd(notifis,username);
    }

    @RequestMapping("delById")
    public R delById(Integer id){
        return notifisService.delById(id);
    }

    @RequestMapping("delAll")
    public R delAll(@RequestParam("ids") Integer[] ids){
        System.out.println("======="+ids);
        return notifisService.delAll(ids);
    }

    @RequestMapping("updateById")
    public R updateById(@RequestBody Notifis notifis){
        return notifisService.updateById(notifis);
    }

}
